package com.servlet;

import com.dao.FindGoodsDao;
import com.dao.imp.FindGoodsDaoImp;
import com.entity.Good;
import net.sf.json.JSONObject;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author Mathison
 * @Title: ${NAME}
 * @Package: ${PACKAGE_NAME}
 * @Description: TODO
 * @date 2021/11/24 17:55
 **/

@WebServlet(name = "GetGoodInfoServlet", value = "/GetGoodInfoServlet")
public class GetGoodInfoServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        int good_id = Integer.parseInt(request.getParameter("good_id"));
//        System.out.println(good_id);
        String sql = "SELECT * FROM goods where good_id = "+good_id;
//        System.out.println(sql);
        FindGoodsDaoImp findGoodsDao = new FindGoodsDaoImp();
        List<Map>goods =  findGoodsDao.findGoods(sql);
        Good good = new Good();
        for (int i = 0; i < goods.size(); i++) {
            good.setSubtitle(goods.get(i).get("subtitle").toString());
            good.setGood_name(goods.get(i).get("good_name").toString());
            good.setGood_price(Double.valueOf(goods.get(i).get("good_price").toString()));
            good.setGood_information(goods.get(i).get("good_information").toString());
            good.setGood_remainingAmount(Integer.valueOf(goods.get(i).get("good_remainingAmont").toString()));
        }
        JSONObject json = JSONObject.fromObject(good);
        response.getWriter().print(json);
    }
}
